#!/bin/bash

source ./func_module.eclass

# TARGET_FILE="target-mso.txt"
TARGET_FILE=$1
SCHEDULE_DIR="/metadata/Redbee"
LINEUP_DIR="/metadata/Redbee/lineups-production"
# SCHEDULE_DATE="20130903"
SCHEDULE_DATE=$2

function main {
  #FILENAME_PATTERN=20130725
  local TARGET_FILE=$1
  local SCHEDULE_DATE=$2
  local SCHEDULE_FILE_XML=""
  #SCHEDULE_FILE="/metadata/Redbee/be/20130903190005samsung.xml"
  local SCHEDULE_FILE_ONLY=""
  local SCHEDULE_FILE=""

  local LINEUP_FILE_XML=""
  #LINEUP_FILE_XML="BE_Telenet_TVA_Lineups_20130530_v8.xml"
  local LINEUP_FILE_ONLY=""
  local LINEUP_FILE=""
  local COUNTRY=""
  local MSO_FILE_PTN=""
  local HEADEND_ID=""
  local DEVICE_TYPE_CODE=""
  local HEADEND_NAME=""

  local NUM_CH_IN_HEADEND=""
  local NUM_CH_IN_HEADEND_WITH_SCHEDULE=""
  local NUM_CH_IN_SCHEDULE=""
  local LINE=""

  local OUTPUTDIR="PROD-HEADENDINFO-${SCHEDULE_DATE}"
  if [ ! -d "$OUTPUTDIR" ]; then
    mkdir $OUTPUTDIR
  fi

  echo "||COUNTRY||NUM CH(S)||CH LANG STAT||MSO ID||MSO NAME||HEADEND ID||DEV CODE||HEADEND NAME||NUM CH||NUM CH(S)||CH LANG STAT||"

  for co_mso_line in `cat ${TARGET_FILE}`
  do
    COUNTRY=`echo $co_mso_line | awk -F "|" '{print $1}'`
    MSO_FILE_PTN=`echo $co_mso_line | awk -F "|" '{print $2}'`

    LINEUP_FILE_XML=$LINEUP_DIR/$COUNTRY/$MSO_FILE_PTN
    LINEUP_FILE_ONLY=`basename $LINEUP_FILE_XML`
    LINEUP_FILE="${LINEUP_FILE_ONLY%.*}"

    SCHEDULE_FILE_XML=`get_latest_schedule_file $COUNTRY $SCHEDULE_DIR $SCHEDULE_DATE $OUTPUTDIR`
    SCHEDULE_FILE_ONLY=`basename $SCHEDULE_FILE_XML`
    SCHEDULE_FILE="${SCHEDULE_FILE_ONLY%.*}"

#    echo "schedule file: " $SCHEDULE_FILE_XML
#    echo "Lineup file: " $LINEUP_FILE_XML

    get_headend_info $LINEUP_FILE_XML > ${OUTPUTDIR}/${LINEUP_FILE}_Lineups.txt
  done
}

main ${TARGET_FILE} ${SCHEDULE_DATE}
